package com.bitejiuyeke.forum.exception;


import com.bitejiuyeke.forum.common.AppResult;
import com.bitejiuyeke.forum.common.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {


    @ResponseBody
    @ExceptionHandler(ApplicationException.class)
    public AppResult handleApplicationException( ApplicationException e) {

        //打印异常
        e.printStackTrace();
        //记录日志
        log.error(e.getMessage());
        if (e.getErrResult() != null)
        {
            return e.getErrResult();
        }
        //非空校验
        if (e.getMessage() == null || e.getMessage().equals("")){
            return AppResult.failed(ResultCode.ERROR_SERVICES);
        }
        //返回具体信息
        return AppResult.failed(e.getMessage());

    }
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public AppResult exceptionHandler( Exception e) {

        //打印异常
        e.printStackTrace();
        //打印日志
        log.error(e.getMessage());
        if (e.getMessage() == null || e.getMessage().equals("")){
            return AppResult.failed(ResultCode.ERROR_SERVICES);
        }
        return AppResult.failed(e.getMessage());

    }








}
